<?php

/**
 * Created by EPP.
 * User: yuxun
 * Date: 18/6/26
 * Time: 21:21
 */
class GiftBagService extends Service{

    public static function indexHeader(){
        $header = array(
        		'id'       =>array('text' => 'ID', 'width' => '50px'),
        		'classify' => array('text' => '类别', 'width' => '150px'),
        		'bag_name' => array('text' => '礼包名称', 'width' => '150px'),
                'bag_desc' => array('text' => '礼包描述', 'width' => '250px'),
                'item'     => array('text' => '礼包物品', 'width' => '300px'),    	    
        		'create_time'   => array('text' => '创建时间','width' => '150px'),
                'action'        => array('text' => '操作', 'width' => '150px'),
        );

        return $header;
    }

    public static function indexData($params){
        $modGiftBag = new ModGiftBag();
        $result = $modGiftBag->page($params);
        
        $classifyList = self::classifyList();
        
        $length = count($result['data']);
        foreach($result['data'] as $k=>&$v){
        	$type = 1;
        	if($k == 0 && $length == 1){  //当前页只剩下一条数据时，删除之后需显示上一页的数据
        		$type = 2;
        	}
        	
        	$v['classify'] = $classifyList[$v['classify']];
        	
        	
        	$item_array = json_decode($v['item'],true);
        	$item = '';
        	foreach ($item_array as $key=>$val){
        		if($val['is_bind'] == 1){
        			$bind = '绑定';
        		}
        		if($val['is_bind'] == 0){
        			$bind = '不绑定';
        		}
        		$item .= $val['item_name'].'-'.$val['item_id'].'-'.$val['item_number'].'-'.$bind;
        		$item .= '</br>';
        	}
        	
        	$v['item'] = $item;
        	$v['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
        	$v['action']         = ' <a href="'.PubFunc::buildBackUrl('/oss/?r=giftBag/delete',$type).'&id='.$v['id'].'" confirm="是否要删除该条信息?">删除</a>';  
        }
        unset($v);

        return $result;
    }
    
    
    public static function doAdd($params){
    	if(PubFunc::checkEmpty($params, array('bag_name','bag_desc'))){
    		return array('state' => false, 'msg' => '缺少必要参数.');
    	}
    	//请求接口，接口成功再入库
    	//$result = self::send($params);
    	$result['state'] = true;
    	if($result['state']){
    		$user = AccAuth::me();
    		$modGiftBag = new ModGiftBag();
    		
    		$person_item = array();
    		foreach ($params['item_id'] as $key=>$val){
    			$data = array();
    			$data['item_name'] = $params['item_name'][$key];
    			$data['item_id'] = $params['item_id'][$key];
    			$data['item_number'] = $params['item_number'][$key];
    			$data['is_bind'] = $params['is_bind'][$key];
    			array_push($person_item, $data);
    		}
    		$item = json_encode($person_item);
    		
    		$modGiftBag->platform = 'test';
    		$modGiftBag->game = $params['g'];
    		$modGiftBag->classify = $params['classify'];
    		$modGiftBag->bag_name = $params['bag_name'];
    		$modGiftBag->bag_desc = $params['bag_desc'];
    		$modGiftBag->item = $item;
    		$modGiftBag->create_user = $user['UserID'];
    		$modGiftBag->create_time = time();
    		$modGiftBag->update_user = $user['UserID'];
    		$modGiftBag->update_time = time();
    		$result = $modGiftBag->save();
    		return array('state' => true, 'msg' =>'添加成功');
    	}else{
    		return array('state' => false, 'msg' => $result['msg']);
    	}
    
    }
    
    public static function delete($id){
    	if(empty($id)) return false;
    
    	$modGiftBag = new ModGiftBag();
    	$modGiftBag->deleteByPk($id);
    	return true;
    }

    public static function send($params){
    	
    }
    
    
    /**
     * 获取礼包礼表
     * @param string $game
     * @param string $key
     * @return multitype:unknown
     */
    public static function getListGiftBag($game=null, $key='id',$classify){ 
    	if(empty($game)){
    		$game = PubBase::getCurrentGame();
    	}
    	
    	if(empty($classify)){
    		$classify = 1;
    	}
    
    	$modGiftBag = new ModGiftBag();
    	$result = $modGiftBag->select(array(
    			'g' => $game,
    			'classify'=>$classify,
    	));
    
    	$options = array();
    	foreach($result as $v){
    		$options[$v[$key]] = $v['bag_name'];
    	}
    	return $options;
    }
    
    public static function classifyList(){
    	$list = array(
    			'1'	 => '普通礼包',
    			'2'  => '通用礼包',
    	);
    	return $list;
    }


}